package com.qlzb.taskmanagement.controller.base.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qlzb.taskmanagement.controller.base.EydBaseController;
import com.qlzb.taskmanagement.pojo.OutputWrapper;
import com.qlzb.taskmanagement.vo.base.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName BaseControllerImpl
 * @Description TODO    基础控制器
 * @Authr 袁聪
 * @Date 2018/10/2 14:25
 * @Version v2.0
 */
public abstract class EydBaseControllerImpl<QueryVo extends BaseQueryVo, FormVo extends BaseFormVo,IdVo extends BaseIdVo, IdListVo extends BaseIdListVo, ActionVo extends BaseActionVo, VerifyVo extends BaseVerifyVo> implements EydBaseController<QueryVo, FormVo, IdVo, IdListVo, ActionVo, VerifyVo> {

    /**
     * 日志
     */
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 模板
     */
    protected String template = "exception/404";

    /**
     * session
     */
    protected final HttpSession session;


    /**
     * 输出包装
     */
    protected OutputWrapper outputWrapper;


    @Autowired
    public EydBaseControllerImpl(HttpSession session) {
        this.session = session;
    }

    /**
     * 设置页面路由信息
     * @param model         model
     * @param baseRoute     baseRoute
     */
    protected void setModelAttributeRoute(Model model, String baseRoute){

        // 当前用户信息信息
        model.addAttribute("activeUser",session.getAttribute("activeUser"));
        // 页面请求url
        model.addAttribute("viewListUrl",baseRoute + "/view-list");             // 列表页面url
        model.addAttribute("viewListUrlData",baseRoute + "/view-list-data");             // 列表页面url
//        model.addAttribute("viewOneUrl",baseRoute + "/view-one");               // 一条数据展示页面url
        model.addAttribute("addViewUrl",baseRoute + "/add-view");               // 添加页面url
        model.addAttribute("addOneUrl",baseRoute + "/add-one");                 // 添加提交url
        model.addAttribute("editViewUrl",baseRoute + "/edit-view");             // 修改页面url
        model.addAttribute("editOneUrl",baseRoute + "/edit-one");               // 修改提交url
        model.addAttribute("previewViewUrl",baseRoute + "/preview-view");       // 预览url
        model.addAttribute("deleteOneUrl",baseRoute + "/delete-one");           // 删除一条数据url
        model.addAttribute("editPasswordUrl",baseRoute + "/edit-password");     // 重置密码url
//        model.addAttribute("deleteListUrl",baseRoute + "/delete-list");         // 删除多条数据url
        model.addAttribute("enableDisableUrl",baseRoute + "/enable-disable");   // 启用/禁用url
        model.addAttribute("verifyDataUrl",baseRoute + "/verify-data");         // 验证数据url

        model.addAttribute("editSortUrl",baseRoute + "/edit-sort");         // 修改排序url

        model.addAttribute("batchCheckUrl",baseRoute + "/batch-check");   //批处理


        model.addAttribute("supplementaryOrderUrl",baseRoute + "/supplementary-order");
//        model.addAttribute("systemadminEditOneUrl",baseRoute+"/edit-one");         // 修改管理员信息url
//        model.addAttribute("systemadminEditOneUrl","/backend/controller/fdfd/edit-one");         // 修改排序url
        // 页面请求权限code
        String baseRoutCode = baseRoute.substring(1,baseRoute.length()).replace("/","@");
        model.addAttribute("viewListCode",baseRoutCode + "@view-list");             // 列表页面code
        model.addAttribute("viewListDataCode",baseRoutCode + "@view-list-data");             // 列表页面code
//        model.addAttribute("viewOneCode",baseRoutCode + "@view-one");               // 一条数据展示页面code
        model.addAttribute("addViewCode",baseRoutCode + "@add-view");               // 添加页面code
        model.addAttribute("addOneCode",baseRoutCode + "@add-one");                 // 添加提交code
        model.addAttribute("editViewCode",baseRoutCode + "@edit-view");             // 修改页面code
        model.addAttribute("editOneCode",baseRoutCode + "@edit-one");               // 修改提交code
        model.addAttribute("previewViewCode",baseRoutCode + "@preview-view");       // 预览code
        model.addAttribute("deleteOneCode",baseRoutCode + "@delete-one");           // 删除一条数据code
//        model.addAttribute("deleteListCode",baseRoutCode + "@delete-list");         // 删除多条数据code
        model.addAttribute("editPasswordCode",baseRoutCode + "@edit-password");           // 重置密码code
        model.addAttribute("enableDisableCode",baseRoutCode + "@enable-disable");   // 启用/禁用code
        model.addAttribute("verifyDataCode",baseRoutCode + "@verify-data");         // 验证数据code
        model.addAttribute("editSortCode",baseRoutCode + "@edit-sort");         // 修改排序code
        model.addAttribute("batchCheckCode",baseRoutCode + "@batch-check"); //处理数据
        model.addAttribute("selectCitysCode",baseRoutCode + "@select-cities");
        model.addAttribute("supplementaryOrderUrl",baseRoutCode + "@supplementary-order");
//        model.addAttribute("PweditOneCode","backend@systemadmin@modifypassword@edit-one");

//        model.addAttribute("admineditOneCode","backend@systemadmin@info@edit-one");

        // 计费相关
        // 添加规则页面
        model.addAttribute("addRegulationViewUrl",baseRoute + "/add-regulation-view");   //添加规则页面
        model.addAttribute("addRegulationViewCode",baseRoutCode + "@add-regulation-view");   //添加规则code

        // 添加模板页面
        model.addAttribute("addTemplateViewUrl",baseRoute + "/add-template-view");   //添加模板页面
        model.addAttribute("addTemplateViewViewCode",baseRoutCode + "@add-template-view");   //添加模板code

        model.addAttribute("addContractAddressUrl",baseRoute + "/add-contract-address-view");  //添加包干用户地址
        model.addAttribute("addContractAddressCode",baseRoutCode + "@add-contract-address-view");   //添加包干用户地址code

        model.addAttribute("addContractUrl",baseRoute + "/add-Contract");  //添加包干用户
        model.addAttribute("addContractCode",baseRoutCode + "@add-Contract");   //添加包干用户code

    }


    @GetMapping("/view-list")
    @Override
    public String viewList(Model model) {
        return template;
    }


    @ResponseBody
    @GetMapping("/list-data")
    @Override
    public OutputWrapper listData(QueryVo vo){
        return null;
    }

    @GetMapping("/view-one")
    @Override
    public String viewOne() {
        return template;
    }

    @ResponseBody
    @GetMapping("/get-one")
    @Override
    public OutputWrapper getOne(IdVo vo){
        return null;
    }

    @GetMapping("/add-view")
    @Override
    public String addView(Model model) {
        return template;
    }

    @ResponseBody
    @PostMapping("/add-one")
    @Override
    public OutputWrapper addOne(@Valid FormVo vo, BindingResult bindingResult, Model model) {
        return null;
    }

    @GetMapping("/edit-view")
    @Override
    public String editView(@Valid IdVo vo, BindingResult bindingResult, Model model) {
        return template;
    }

    @ResponseBody
    @PostMapping("/edit-one")
    @Override
    public OutputWrapper editOne(@Valid FormVo vo, BindingResult bindingResult, Model model) {
        return null;
    }

    @ResponseBody
    @PostMapping("/edit-password")
    @Override
    public OutputWrapper editPassword(@Valid IdVo vo, BindingResult bindingResult, Model model) {
        return outputWrapper;
    }

    @ResponseBody
    @PostMapping("/delete-one")
    @Override
    public OutputWrapper deleteOne(@Valid IdVo vo, BindingResult bindingResult, Model model) {
        return outputWrapper;
    }

    @ResponseBody
    @PostMapping("/delete-list")
    @Override
    public OutputWrapper deleteList(@Valid IdListVo vo, BindingResult bindingResult, Model model) {
        return outputWrapper;
    }

    @GetMapping("/preview-view")
    @Override
    public String previewView(@Valid IdVo vo, BindingResult bindingResult, Model model) {
        return template;
    }




    @ResponseBody
    @PostMapping("/enable-disable")
    @Override
    public OutputWrapper enableDisable(@Valid ActionVo vo, BindingResult bindingResult, Model model) {
        return outputWrapper;
    }

    @ResponseBody
    @GetMapping("/verify-data")
    @Override
    public Map<String, Boolean> verifyData(@Valid VerifyVo vo, BindingResult bindingResult, Model model) {
        Map<String, Boolean> map = new HashMap<>();

        boolean flag = model.containsAttribute("flag");

        map.put("valid",flag);

        return map;
    }


    @ResponseBody
    @PostMapping("/edit-sort")
    @Override
    public OutputWrapper editSort(@Valid BaseSortFormVo vo, BindingResult bindingResult, Model model) {
        return outputWrapper;
    }
}
